package com.onlyphoto.onlymall.mvp.view.fragment

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseFragment
import com.onlyphoto.onlymall.mvp.contract.COrders
import com.onlyphoto.onlymall.mvp.presenter.POrdersImpl

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.bean.OrderFooterBean
import com.onlyphoto.onlymall.bean.response.OrderBean
import com.onlyphoto.onlymall.mvp.view.binder.NoMoreBinder
import com.onlyphoto.onlymall.mvp.view.binder.OrderListFooterBinder
import com.onlyphoto.onlymall.mvp.view.binder.OrderListHeaderBinder
import com.onlyphoto.onlymall.mvp.view.binder.OrderItemBinder
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.fragment_orders.*
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter


class OrdersFragment : BaseFragment<POrdersImpl>(), COrders.IVOrders, OnRefreshListener, OnLoadmoreListener {

    var adapter: MultiTypeAdapter? = null
    var layoutManager: LinearLayoutManager? = null
    private var orderStatus: String? = null
    private var items: Items? = null
    var pageNum = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = arguments
        orderStatus = bundle?.getString(TAG_ORDER_STATUS, "0")
    }

    override fun retry() {}

    override fun init() {
        adapter = MultiTypeAdapter()
        layoutManager = LinearLayoutManager(activity)
        layoutManager?.orientation = RecyclerView.VERTICAL
        adapter?.register(OrderBean::class.java, OrderListHeaderBinder())
        adapter?.register(OrderBean.GoodsInfo::class.java, OrderItemBinder())
        adapter?.register(OrderFooterBean::class.java, OrderListFooterBinder())
        adapter?.register(String::class.java, NoMoreBinder())
        items = Items()
        adapter?.items = items!!
        rv_orders.adapter = adapter
        rv_orders.layoutManager = layoutManager

        loadData()
        initListener()
    }

    private fun initListener(){
        srl_order.setOnRefreshListener(this)
        srl_order.setOnLoadmoreListener(this)
    }

    private fun loadData(){
        orderStatus?.let { mPresenter!!.getOrders(it.toInt(), pageNum) }
    }

    override fun onRefresh(refreshlayout: RefreshLayout?) {
        pageNum = 1
        srl_order.isEnableLoadmore = true
        loadData()
    }

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        pageNum++
        loadData()
    }

    override fun showEmpty() {
        srl_order.finishRefresh()
        items?.clear()
        adapter?.notifyDataSetChanged()
    }

    override fun showNoMore() {
        srl_order.finishLoadmore()
        srl_order.isEnableLoadmore = false
        items?.add("没有更多订单了～")
        adapter?.notifyDataSetChanged()
    }

    override fun getOrdersSuccess(list: ArrayList<OrderBean>) {
        srl_order.finishRefresh()
        srl_order.finishLoadmore()
        if (pageNum == 1) {
            items?.clear()
        }
        for (orderBean in list) {
            items?.add(orderBean)
            var count = 0
            for (goodsInfo in orderBean.goods_info){
                items?.add(goodsInfo)
                if (goodsInfo.gift_set == 1) {
                    val gift = goodsInfo.gift_arr!!
                    val giftGoods = OrderBean.GoodsInfo(gift.goods_id, gift.title, gift.buy_num, 0.0, gift.front_img, gift.style, gift.size, gift.color)
                    items?.add(giftGoods)
                    count += giftGoods.buy_num
                }
                count += goodsInfo.buy_num
            }
            val bean = OrderFooterBean(orderBean.id, count, orderBean.total_price)
            items?.add(bean)
        }
        adapter?.notifyDataSetChanged()
    }

    override fun getLayoutRes(): Int {
        return R.layout.fragment_orders
    }

    override fun createPresenter() {
        mPresenter = POrdersImpl(mContext, this)
    }

    override fun showLoading() {
        showDefaultDialog()
    }

    override fun hideLoading() {
        hideDefaultDialog()
    }

    companion object {

        //argument传进来的订单type
        private const val TAG_ORDER_STATUS = "order_status"

        fun newInstance(status: String): OrdersFragment {
            val fragment = OrdersFragment()
            val args = Bundle()
            args.putString(TAG_ORDER_STATUS, status)
            fragment.arguments = args
            return fragment
        }
    }

}
